package com.aiyige.page.filter.util;

import android.text.TextUtils;

import com.aiyige.model.commontag.CommonTag;
import com.aiyige.model.commontag.util.CommonTagUtil;
import com.aiyige.page.filter.model.Filter;
import com.aiyige.page.filter.model.FilterResponse;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

public class FilterUtil {
    public static final int MAX_MULTI_SELECT_NUM = 3;


    public static List<Filter> extractActiveFilter(List<Filter> source) {
        List<Filter> activeFilterList = new LinkedList<>();
        for (Filter filter : source) {
            switch (filter.getType()) {
                case Filter.TYPE_MULTI_SELECT_BIG_RECT:
                case Filter.TYPE_MULTI_SELECT_SMALL_RECT:
                case Filter.TYPE_SINGLE_SELECT_BIG_RECT:
                case Filter.TYPE_SINGLE_SELECT_SMALL_RECT: {
                    List<CommonTag> selectedTagList = CommonTagUtil.extractSelected(filter.getTagList());
                    if (!ListUtil.isEmpty(selectedTagList)) {
                        Filter activeFilter = Filter.newBuilder()
                                .type(filter.getType())
                                .id(filter.getId())
                                .name(filter.getName())
                                .tagList(selectedTagList)
                                .build();
                        activeFilterList.add(activeFilter);
                    }
                    break;
                }
                case Filter.TYPE_ADDRESS: {
                    if (!TextUtils.isEmpty(filter.getCity()) && !TextUtils.isEmpty(filter.getCity()) && !TextUtils.isEmpty(filter.getProvince())) {
                        Filter activeFilter = Filter.newBuilder()
                                .type(filter.getType())
                                .id(filter.getId())
                                .name(filter.getName())
                                .city(filter.getCity())
                                .cityId(filter.getCityId())
                                .province(filter.getProvince())
                                .build();
                        activeFilterList.add(activeFilter);
                    }
                    break;
                }
                case Filter.TYPE_TIME_RANGE: {
                    if (filter.getStartTime() != null || filter.getEndTime() != null) {
                        Filter activeFilter = Filter.newBuilder()
                                .id(filter.getId())
                                .name(filter.getName())
                                .type(filter.getType())
                                .startTime(filter.getStartTime())
                                .endTime(filter.getEndTime())
                                .build();
                        activeFilterList.add(activeFilter);
                    }
                    break;
                }
            }
        }
        return activeFilterList;
    }

    public static boolean safeEquals(List<Filter> listA, List<Filter> listB) {
        if (ListUtil.isEmpty(listA) && ListUtil.isEmpty(listB)) {
            return true;
        } else if (ListUtil.isEmpty(listA) || ListUtil.isEmpty(listB)) {
            return false;
        } else {
            if (listA.size() != listB.size()) {
                return false;
            } else {
                for (Filter item : listA) {
                    if (!listB.contains(item)) {
                        return false;
                    }
                }
                return true;
            }
        }
    }

    public static void prepare(List<Filter> totalFilterList, List<Filter> activeFilterList) {
        if (ListUtil.isEmpty(totalFilterList)) return;
        clearSelect(totalFilterList);
        resumeSelect(totalFilterList, activeFilterList);
    }

    public static void resumeSelect(List<Filter> totalFilterList, List<Filter> activeFilterList) {
        if (ListUtil.isEmpty(totalFilterList) || ListUtil.isEmpty(activeFilterList)) return;
        for (Filter activeFilter : activeFilterList) {
            for (Filter filter : totalFilterList) {
                if (StringUtils.safeEquals(activeFilter.getId(), filter.getId()) && activeFilter.getType() == filter.getType()) {
                    switch (activeFilter.getType()) {
                        case Filter.TYPE_MULTI_SELECT_BIG_RECT:
                        case Filter.TYPE_MULTI_SELECT_SMALL_RECT:
                        case Filter.TYPE_SINGLE_SELECT_SMALL_RECT:
                        case Filter.TYPE_SINGLE_SELECT_BIG_RECT:
                            CommonTagUtil.resumeSelect(filter.getTagList(), activeFilter.getTagList());
                            break;
                        case Filter.TYPE_ADDRESS:
                            filter.setCity(activeFilter.getCity());
                            filter.setProvince(activeFilter.getProvince());
                            filter.setCityId(activeFilter.getCityId());
                            break;
                        case Filter.TYPE_TIME_RANGE:
                            filter.setStartTime(activeFilter.getStartTime());
                            filter.setEndTime(activeFilter.getEndTime());
                            break;
                    }
                }
            }
        }
    }

    public static void clearSelect(List<Filter> totalFilterList) {
        if (ListUtil.isEmpty(totalFilterList)) return;
        for (Filter filter : totalFilterList) {
            switch (filter.getType()) {
                case Filter.TYPE_MULTI_SELECT_BIG_RECT:
                case Filter.TYPE_MULTI_SELECT_SMALL_RECT:
                case Filter.TYPE_SINGLE_SELECT_BIG_RECT:
                case Filter.TYPE_SINGLE_SELECT_SMALL_RECT:
                    CommonTagUtil.clearSelect(filter.getTagList());
                    break;
                case Filter.TYPE_ADDRESS:
                    filter.setProvince("");
                    filter.setCity("");
                    filter.setCityId("");
                    break;
                case Filter.TYPE_TIME_RANGE:
                    filter.setStartTime(null);
                    filter.setEndTime(null);
                    break;
            }
        }
    }

    public static List<Filter> parse(List<FilterResponse> filterResponseList) {
        List<Filter> filterList = new LinkedList<>();
        if (ListUtil.isEmpty(filterResponseList)) return filterList;
        for (FilterResponse filterResponse : filterResponseList) {
            if (TextUtils.isEmpty(filterResponse.getName())
                    || TextUtils.isEmpty(filterResponse.getFormType())
                    || TextUtils.isEmpty(filterResponse.getStyle()))
                continue;
            switch (filterResponse.getFormType()) {
                case FilterResponse.FORM_TYPE_ADDRESS: {
                    filterList.add(Filter.newBuilder()
                            .id(filterResponse.getName())
                            .name(filterResponse.getLabel())
                            .type(Filter.TYPE_ADDRESS)
                            .build());
                    break;
                }
                case FilterResponse.FORM_TYPE_DATE: {
                    filterList.add(Filter.newBuilder()
                            .id(filterResponse.getName())
                            .name(filterResponse.getLabel())
                            .type(Filter.TYPE_TIME_RANGE)
                            .build());
                    break;
                }
                case FilterResponse.FORM_TYPE_MULTIPLE_SELECT: {
                    try {
                        List<FilterResponse.Choice> choiceList = JSON.parseArray(filterResponse.getChoises(), FilterResponse.Choice.class);
                        if (ListUtil.isEmpty(choiceList)) continue;
                        List<CommonTag> commonTagList = new LinkedList<>();
                        for (FilterResponse.Choice choice : choiceList) {
                            commonTagList.add(CommonTag.newBuilder()
                                    .id(choice.getId())
                                    .title(choice.getName())
                                    .selected(false)
                                    .build());
                        }
                        switch (filterResponse.getStyle()) {
                            case FilterResponse.STYLE_BIG_RECT:
                                filterList.add(Filter.newBuilder()
                                        .type(Filter.TYPE_MULTI_SELECT_BIG_RECT)
                                        .id(filterResponse.getName())
                                        .name(filterResponse.getLabel())
                                        .tagList(commonTagList)
                                        .build());
                                break;
                            case FilterResponse.STYLE_SMALL_RECT:
                                filterList.add(Filter.newBuilder()
                                        .type(Filter.TYPE_MULTI_SELECT_SMALL_RECT)
                                        .id(filterResponse.getName())
                                        .name(filterResponse.getLabel())
                                        .tagList(commonTagList)
                                        .build());
                                break;
                        }
                    } catch (Exception e) {

                    }
                    break;
                }
                case FilterResponse.FORM_TYPE_SINGLE_SELECT: {
                    try {
                        List<FilterResponse.Choice> choiceList = JSON.parseArray(filterResponse.getChoises(), FilterResponse.Choice.class);
                        if (ListUtil.isEmpty(choiceList)) continue;
                        List<CommonTag> commonTagList = new LinkedList<>();
                        for (FilterResponse.Choice choice : choiceList) {
                            commonTagList.add(CommonTag.newBuilder()
                                    .id(choice.getId())
                                    .title(choice.getName())
                                    .selected(false)
                                    .build());
                        }
                        switch (filterResponse.getStyle()) {
                            case FilterResponse.STYLE_BIG_RECT:
                                filterList.add(Filter.newBuilder()
                                        .type(Filter.TYPE_SINGLE_SELECT_BIG_RECT)
                                        .id(filterResponse.getName())
                                        .name(filterResponse.getLabel())
                                        .tagList(commonTagList)
                                        .build());
                                break;
                            case FilterResponse.STYLE_SMALL_RECT:
                                filterList.add(Filter.newBuilder()
                                        .type(Filter.TYPE_SINGLE_SELECT_SMALL_RECT)
                                        .id(filterResponse.getName())
                                        .name(filterResponse.getLabel())
                                        .tagList(commonTagList)
                                        .build());
                                break;
                        }
                    } catch (Exception e) {

                    }
                    break;
                }


            }
        }
        return filterList;
    }
}
